/**
 * 2016，thor.com，all rights reserved。
 * 
 * Project：		palmmall-core
 * Filename：	AComplaintServiceImpl.java
 * Created：		2016年8月21日
 * Creator：		wolv
 */
package com.thor.palmmall.rest.complaint;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.thor.commons.entity.BeanOperateInfo;
import com.thor.commons.exception.ThorServiceException;
import com.thor.commons.query.QueryResult;
import com.thor.commons.util.Assert;
import com.thor.palmmall.rest.common.AContractQueryParam;
import com.thor.palmmall.rest.common.AQueryCommonParam;
import com.thor.palmmall.rest.common.AUserGroupLoadParam;
import com.thor.palmmall.rest.common.SContractQueryParamConverter;
import com.thor.palmmall.rest.common.SUserGroupLoadParamConverter;
import com.thor.palmmall.rest.complaint.converter.AComplaintConverter;
import com.thor.palmmall.rest.complaint.converter.SComplaint4CreateConverter;
import com.thor.palmmall.rest.complaint.converter.SComplaintConverter;
import com.thor.palmmall.rest.complaint.converter.SComplaintQueryParamConverter;
import com.thor.palmmall.rest.complaint.converter.SWorkRecordConverter;
import com.thor.palmmall.rest.contract.AContract;
import com.thor.palmmall.rest.contract.AQueryContractParam;
import com.thor.palmmall.rest.contract.converter.AContractConverter;
import com.thor.palmmall.rest.contract.converter.SQueryContractParamConverter;
import com.thor.palmmall.spi.common.SOperateInfoConverter;
import com.thor.palmmall.spi.common.SQueryResult;
import com.thor.palmmall.spi.common.SQueryResultConverter;
import com.thor.palmmall.spi.common.SResponse;
import com.thor.palmmall.spi.complaint.SComplaint;
import com.thor.palmmall.spi.complaint.SComplaintService;
import com.thor.palmmall.spi.contract.SContract;
import com.thor.palmmall.spi.contract.SContractService;

/**
 * @author wolv
 *
 */
public class AComplaintServiceImpl implements AComplaintService {

	@Override
	public String create(AComplaint complaint, BeanOperateInfo operateInfo) throws ThorServiceException {
		Assert.assertArgumentNotNull(complaint, "complaint");
		Assert.assertArgumentNotNull(operateInfo, "operateInfo");
		Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
		Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");

		SResponse<String> resp = sComplaintService.create(SComplaint4CreateConverter.getInstance().convert(complaint), SOperateInfoConverter
				.getInstance().convert(operateInfo));
		if (resp.isSuccess())
			return resp.getBody();
		else
			throw new ThorServiceException("无法创建投诉单：" + resp.getMessage());
	}

	@Override
	public AComplaint get(String id, AUserGroupLoadParam param) throws ThorServiceException {
		SResponse<SComplaint> resp = sComplaintService.get(id, SUserGroupLoadParamConverter.getInstance().convert(param));
		if (resp.isSuccess())
			return AComplaintConverter.getInstance().convert(resp.getBody());
		else
			throw new ThorServiceException("无法找到指定ID的投诉单：" + resp.getMessage());
	}

//	@Override
//	public void addWorkRecord(AWorkRecord record, BeanOperateInfo operateInfo) throws ThorServiceException {
//		Assert.assertArgumentNotNull(record, "record");
//		Assert.assertArgumentNotNull(operateInfo, "operateInfo");
//		Assert.assertArgumentNotNull(operateInfo.getOperator(), "operateInfo.getOperator()");
//		Assert.assertArgumentNotNull(operateInfo.getTime(), "operateInfo.getTime()");
//
//		SResponse<Void> resp = sComplaintService.save(SWorkRecordConverter.getInstance().convert(record), SOperateInfoConverter.getInstance()
//				.convert(operateInfo));
//		if (resp.isSuccess() == false)
//			throw new ThorServiceException("无法保存投诉单：" + resp.getMessage());
//	}

	@Override
	public QueryResult<AComplaint> query(AQueryCommonParam param) throws ThorServiceException {
		Assert.assertArgumentNotNull(param, "param");

		SResponse<SQueryResult<SComplaint>> resp = sComplaintService.query(SComplaintQueryParamConverter.getInstance().convert(param));
		if (resp.isSuccess())
			return SQueryResultConverter.convert(resp.getBody(), AComplaintConverter.getInstance());
		else
			throw new ThorServiceException("无法查询投诉单：" + resp.getMessage());
	}
	

	@Override
	public List<String> queryOutgoingState() throws ThorServiceException {
	   SResponse<List<String>> resp = sComplaintService.queryOutgoingState();
       if (resp.isSuccess()){
           return resp.getBody();
       }else{
           throw new ThorServiceException("无法创建投诉单：" + resp.getMessage());
       }
	}

	@Override
	public QueryResult<AContract> queryContract(AQueryContractParam param) throws ThorServiceException {
		Assert.assertArgumentNotNull(param, "param");

		SResponse<SQueryResult<SContract>> resp = sContractService.query(SQueryContractParamConverter.getInstance().convert(param));
		if (resp.isSuccess())
			return SQueryResultConverter.convert(resp.getBody(), AContractConverter.getInstance());
		else
			throw new ThorServiceException("无法查询合同：" + resp.getMessage());
	}
	
	@Override
    public void save(AComplaint complaint, BeanOperateInfo operateInfo) throws ThorServiceException {
	  Assert.assertArgumentNotNull(complaint, "complaint");
      Assert.assertArgumentNotNull(operateInfo, "operateInfo");


      SResponse<Void> resp = sComplaintService.save(SComplaintConverter.getInstance().convert(complaint),
          SOperateInfoConverter.getInstance().convert(operateInfo));
      if (resp.isSuccess() == false){
          throw new ThorServiceException("无法保存投诉单：" + resp.getMessage());
      }
    }


  @Autowired
	private SComplaintService sComplaintService;


	@Autowired
	private SContractService sContractService;
}
